using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;
using CE.Security;

public partial class SiteUserSecureLogin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] != null && Session[CE.Global.General.UserTypeId] != null)
        {
            if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) ==
                Convert.ToInt32(CE.Global.CEUserType.SiteUser))
            {
                Response.Redirect("SiteUserHome.aspx");
            }
            //else if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) ==
            //    Convert.ToInt32(CE.Global.CEUserType.SiteUserUAT))
            //{
            //    Response.Redirect("~/Car_Insurance/Profile.aspx");
            //}
        }
    }

    protected void btnSignIn_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            AccountDB _accountdb = new AccountDB();
            Account _account = _accountdb.CheckEmail(txtSiteUserEmailAddress.Text.ToLower(), Convert.ToInt32(CE.Global.CEUserType.SiteUser));
            if (_account != null)
            {
                //DataSet _dsAccountStatus = new DataSet();
                AccountStatus _accountstatus = new AccountStatus();
                AccountStatusDB _accountstatusdb = new AccountStatusDB();
                //_dsAccountStatus = _accountstatusdb.GetAccountStatus(txtSiteUserEmailAddress.Text.ToLower(),txtSiteUserAuthCode.Text.Trim());
                _accountstatus = _accountstatusdb.GetAccountStatus(_account.CEID, 200);
                //if (_dsAccountStatus != null)
                if(_accountstatus != null)
                {
                    //if(Convert.ToBoolean(_dsAccountStatus.Tables[1].Rows[0]["IsBlock"].ToString()) == false)
                    if (_accountstatus.IsBlock == false)
                    {
                        //if (MySecurity.Encrypt(txtSiteUserAuthCode.Text.Trim(), true) == _dsAccountStatus.Tables[0].Rows[0]["AuthCode"].ToString())
                        if (txtSiteUserAuthCode.Text == _account.AuthCode)
                        {
                            if (_account.Info1 == "0")
                            {
                                //Reset Email Fail Message
                                _account.Info1 = "";
                                _account.Info2 = "";
                                _accountdb.Insert(_account);
                            }

                            _accountstatusdb.InsertAccountStatus(_account.CEID, 200, 1);
                            DateTime dtNow = new DateTime();
                            dtNow = DateTime.Now;

                            Session[CE.Global.General.UserId] = _account.CEID;
                            Session[CE.Global.General.UserTypeId] = _account.UserTypeId;
                            Session[CE.Global.General.UserEmail] = _account.Email;
                            Session[CE.Global.General.StartTime] = dtNow.ToString();

                            ProfileDB _profiledb = new ProfileDB();
                            Profile _profile = new Profile();
                            Contact _contact = new Contact();
                            ContactDB _contactdb = new ContactDB();

                            _contact = _contactdb.GetContact(_account.CEID);

                            _profile = _profiledb.GetProfile(_account.CEID);

                            if (_contact != null)
                            {
                                Session[CE.Global.General.UserPhoneNo] = _contact.OfficePhone;
                            }
                            else
                            {
                                Session[CE.Global.General.UserPhoneNo] = null;
                            }

                            if (_profile != null)
                            {
                                Session[CE.Global.General.UserName] = _profile.Salutation + " " + _profile.SurName;
                                if (_profile.Status.ToLower() == "complete")
                                {
                                    Session[CE.Global.General.UserProfileAndContactComplete] = "1";
                                }
                                else
                                {
                                    Session[CE.Global.General.UserProfileAndContactComplete] = "0";
                                }
                            }
                            else
                            {
                                Session[CE.Global.General.UserName] = "No Name";
                            }

                            SaveUserLoginInformation(_account.CEID, _account.UserTypeId);
                            Response.Redirect("~/SiteUserHome.aspx");

                            //SiteUserCompanyLink _comlink = new SiteUserCompanyLink();
                            //SiteUserCompanyLinkDB _comlinkdb = new SiteUserCompanyLinkDB();
                            //_comlink = _comlinkdb.GetSiteUserCompanyId(_account.CEID);
                            //if (_comlink != null)
                            //{
                            //    Session[CE.Global.General.SiteUserCompanyId] = _comlink.SiteUserCompanyId.ToString();
                            //    if (_account.UserTypeId == Convert.ToInt32(CE.Global.CEUserType.SiteUser))
                            //    {
                            //        if (_comlink.SiteUserCompanyId == 29 || _comlink.SiteUserCompanyId == 30 || _comlink.SiteUserCompanyId == 23 || _comlink.SiteUserCompanyId == 31 ||
                            //            _comlink.SiteUserCompanyId == 24 || _comlink.SiteUserCompanyId == 34 || _comlink.SiteUserCompanyId == 35)
                            //        {
                            //            SaveUserLoginInformation(_account.CEID, _account.UserTypeId);
                            //            //Response.Redirect("~/SiteUser/CarInsurance/LibertyInsuranceHome.aspx");
                            //            Response.Redirect("~/SiteUser/SiteUserHome.aspx");
                            //        }
                            //        else if (_comlink.SiteUserCompanyId == 28)
                            //        {
                            //            SaveUserLoginInformation(_account.CEID, _account.UserTypeId);
                            //            Response.Redirect("~/SiteUser/HomeRefinancing/HFHome.aspx");
                            //        }
                            //    }
                            //    else
                            //    {
                            //        Response.Redirect("~/Car_Insurance/Profile.aspx");
                            //    }
                            //}
                            //else
                            //{
                            //    lblMessage.Text = "No company found associated with this SiteUser account.";
                            //}
                        }
                        else
                        {
                            _accountstatusdb.InsertAccountStatus(_account.CEID, 200, 0);
                            lblMessage.Text = "Incorrect Password.";
                        }
                    }
                    else
                    {
                        lblMessage.Text = "Account Blocked. <br/> Contact: support@comparexpress.com";
                    }
                }
            }
            else
            {
                lblMessage.Text = "Email address is not found.";
            }
        }

        #region Comment 
        /******************************************************************************Sakthi
        if (IsValid)
        {
            AccountDB _accountdb = new AccountDB();
            Account _account = _accountdb.CheckEmail(txtSiteUserEmailAddress.Text.ToLower(), Convert.ToInt32(CE.Global.CEUserType.SiteUser));

            //if (_account == null)
            //{
            //    _account = _accountdb.CheckEmail(txtSiteUserEmailAddress.Text, Convert.ToInt32(CE.Global.CEUserType.SiteUserUAT));
            //}

            if (_account != null)
            {
                DataSet _dsAccountStatus = new DataSet();
                AccountStatus _accountstatus = new AccountStatus();
                AccountStatusDB _accountstatusdb = new AccountStatusDB();
                _dsAccountStatus = _accountstatusdb.GetAccountStatus(txtSiteUserEmailAddress.Text.ToLower());
                if (_dsAccountStatus != null)
                {
                    if(Convert.ToBoolean(_dsAccountStatus.Tables[1].Rows[0]["IsBlock"].ToString()) == false)
                    {
                    //if (txtSiteUserAuthCode.Text == _account.AuthCode)
                    if (MySecurity.Encrypt(txtSiteUserAuthCode.Text.Trim(), true) == _dsAccountStatus.Tables[0].Rows[0]["AuthCode"].ToString())
                    {
                        //AccountStatus _accountstatus = new AccountStatus();
                        //AccountStatusDB _accountstatusdb = new AccountStatusDB();
                        //_accountstatus = _accountstatusdb.GetAccountStatus(_account.CEID);
                        //if (_accountstatus != null)
                        {
                            //if (_accountstatus.IsBlock == false)
                            //{
                                _accountstatusdb.InsertAccountStatus(_account.CEID, 1);
                                DateTime dtNow = new DateTime();
                                dtNow = DateTime.Now;

                                Session[CE.Global.General.UserId] = _account.CEID;
                                Session[CE.Global.General.UserTypeId] = _account.UserTypeId;
                                Session[CE.Global.General.UserEmail] = _account.Email;
                                Session[CE.Global.General.StartTime] = dtNow.ToString();

                                ProfileDB _profiledb = new ProfileDB();
                                Profile _profile = new Profile();
                                Contact _contact = new Contact();
                                ContactDB _contactdb = new ContactDB();

                                _contact = _contactdb.GetContact(_account.CEID);

                                _profile = _profiledb.GetProfile(_account.CEID);

                                if (_contact != null)
                                {
                                    Session[CE.Global.General.UserPhoneNo] = _contact.HomePhone;
                                }
                                else
                                {
                                    Session[CE.Global.General.UserPhoneNo] = "No Phone No";
                                }

                                if (_profile != null)
                                {
                                    Session[CE.Global.General.UserName] = _profile.Salutation + " " + _profile.SurName;
                                    if (_profile.Status.ToLower() == "complete")
                                    {
                                        Session[CE.Global.General.UserProfileAndContactComplete] = "1";
                                    }
                                    else
                                    {
                                        Session[CE.Global.General.UserProfileAndContactComplete] = "0";
                                    }
                                }
                                else
                                {
                                    Session[CE.Global.General.UserName] = "No Name";
                                }


                                //LoginHistory _loghis = new LoginHistory();
                                //_loghis.CEID = _account.CEID;
                                //_loghis.UserTypeId = _account.UserTypeId;
                                //_loghis.StartTime = dtNow;
                                //_loghis.EndTime = dtNow;
                                //_loghis.IsExpired = false;
                                //_loghis.Status = "LOGIN";
                                //LoginHistoryDB _loghisdb = new LoginHistoryDB();
                                //Session[CE.Global.General.LoginHistoryId] = _loghisdb.Insert(_loghis).ToString();

                                SiteUserCompanyLink _comlink = new SiteUserCompanyLink();
                                SiteUserCompanyLinkDB _comlinkdb = new SiteUserCompanyLinkDB();
                                _comlink = _comlinkdb.GetSiteUserCompanyId(_account.CEID);
                                if (_comlink != null)
                                {
                                    Session[CE.Global.General.SiteUserCompanyId] = _comlink.SiteUserCompanyId.ToString();
                                    if (_account.UserTypeId == Convert.ToInt32(CE.Global.CEUserType.SiteUser))
                                    {
                                        if (_comlink.SiteUserCompanyId == 29 || _comlink.SiteUserCompanyId == 30 || _comlink.SiteUserCompanyId == 23 || _comlink.SiteUserCompanyId == 31 || _comlink.SiteUserCompanyId == 24)
                                        {
                                            SaveUserLoginInformation(_account.CEID, _account.UserTypeId);
                                            //Response.Redirect("~/SiteUser/CarInsurance/LibertyInsuranceHome.aspx");
                                            Response.Redirect("~/SiteUser/SiteUserHome.aspx");
                                        }
                                        else if (_comlink.SiteUserCompanyId == 28)
                                        {
                                            SaveUserLoginInformation(_account.CEID, _account.UserTypeId);
                                            Response.Redirect("~/SiteUser/HomeRefinancing/HFHome.aspx");
                                        }
                                    }
                                    else
                                    {
                                        Response.Redirect("~/Car_Insurance/Profile.aspx");
                                    }
                                }
                                else
                                {
                                    lblMessage.Text = "No company found associated with this SiteUser account.";
                                }
                            //}
                            //else
                            //{
                            //    lblMessage.Text = "Account Blocked.";
                            //}
                        //}
                        //else
                        //{
                        //    _accountstatusdb.InsertAccountStatus(_account.CEID, 1);
                        //}
                    //}
                //}
                        
                //else
                //{
                //    //AccountStatusDB _accountstatusdb = new AccountStatusDB();
                //    _accountstatusdb.InsertAccountStatus(_account.CEID, 0);
                //    lblMessage.Text = "Incorrect Password.";
                //}
                    }
                    else
                    {
                        lblMessage.Text = "Account Blocked.";
                    }
                }
            }
            else
            {
                lblMessage.Text = "Email address is not found.";
            }
        }*/
        #endregion
    }


    protected bool SaveUserLoginInformation(Int64 CEID, int UserTypeId)
    {
        LoginHistory _loghis = new LoginHistory();
        LoginHistory _lastloghis = new LoginHistory();
        LoginHistoryDB _loghisdb = new LoginHistoryDB();
        LoginInformation _loginfo = new LoginInformation();
        LoginInformationDB _loginfodb = new LoginInformationDB();

        _lastloghis = _loghisdb.GetLastLoginHistory(CEID, UserTypeId);
        if (_lastloghis != null)
        {
            if (_lastloghis.StartTime != null)
            {
                Session[CE.Global.General.LastLoginTime] = _lastloghis.StartTime;
            }
            else
            {
                Session[CE.Global.General.LastLoginTime] = DateTime.Now;
            }
        }
        else
        {
            Session[CE.Global.General.LastLoginTime] = DateTime.Now;
        }

        _loghis.CEID = CEID;
        _loghis.UserTypeId = UserTypeId;
        _loghis.StartTime = DateTime.Now;
        _loghis.EndTime = DateTime.Now;
        _loghis.IsExpired = false;
        _loghis.Status = "LOGIN";

        _loghis.LoginHisId = _loghisdb.Insert(_loghis);

        if (_loghis.LoginHisId > 0)
        {
            Session[CE.Global.General.LoginHistoryId] = _loghis.LoginHisId.ToString();
            _loginfo.CEID = CEID;
            _loginfo.UserTypeId = UserTypeId;
            _loginfo.LogHisId = _loghis.LoginHisId;

            _loginfo.Browser = Request.Browser.Browser;
            _loginfo.BrowserType = Request.Browser.Type;
            _loginfo.BrowserVersion = Request.Browser.Version;
            _loginfo.MajorVersion = Request.Browser.MajorVersion.ToString();
            _loginfo.MinorVersion = Request.Browser.MinorVersionString;

            _loginfo.JavaScript = Request.Browser.JavaScript;
            _loginfo.VBScript = Request.Browser.VBScript;
            _loginfo.ActivX = Request.Browser.ActiveXControls;
            _loginfo.Frames = Request.Browser.Frames;
            _loginfo.Tables = Request.Browser.Tables;
            _loginfo.Cookies = Request.Browser.Cookies;
            _loginfo.DotNetVersion = Request.Browser.ClrVersion.ToString();
            _loginfo.JavaApplet = Request.Browser.JavaApplets;

            _loginfo.Win16 = Request.Browser.Win16;
            _loginfo.Win32 = Request.Browser.Win32;
            _loginfo.Platform = Request.Browser.Platform;
            _loginfo.UserAgent = Request.UserAgent.ToString();
            _loginfo.IpAddress = Request.UserHostAddress;
            _loginfo.HostName = Request.UserHostName;

            //_loginfo.Info1 = Request.Browser.Tables;
            //_loginfo.Info2 = Request.Browser.Platform;
            _loginfo.UserInfoId = _loginfodb.Insert(_loginfo);
        }

        return _loginfo.UserInfoId > 0;

    }
}
